/**
  * @(#)AutoLogManager.java
  *
  * Automatically find and detect the slf4j binding library available on the classpath.
  *
  * @author Course Scheduler Team
  * 
  * @license GNU General Public License version 3 (GPLv3)
  *
  * This file is part of Course Scheduler, an open source, cross platform
  * course scheduling tool, configurable for most universities.
  *
  * Copyright (C) 2010-2012 Course Scheduler Team
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * 
  */
package com.pollicitus.scheduler.logging;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pollicitus.scheduler.core.Manager;
import com.pollicitus.scheduler.core.NamespaceSettings;
import com.pollicitus.scheduler.core.Settings;
import com.pollicitus.scheduler.logging.LogManager;

/**
 * Automatically find and detect the slf4j binding library available on the classpath.
 * 
 * @author Course Scheduler Team
 *
 */
public final class AutoLogManager extends LogManager {
	
	//redirected log manager and namespace settings
	private LogManager redirect;
	private NamespaceSettings logSettings;
	
	/* (non-Javadoc)
	 * @see com.scheduler.logging.LogManager#initialize(com.scheduler.core.Settings)
	 */
	@Override
	public void initialize(Settings settings) throws InstantiationException, IllegalAccessException, ClassNotFoundException, FileNotFoundException, IOException {
		logSettings = new NamespaceSettings(settings, namespace);
		
		//retrieve a logger using the default settings, this logger will be used to detect the binding library
		Logger test = LoggerFactory.getLogger(this.getClass());
		@SuppressWarnings("unchecked")
		Class<Logger> clazz = (Class<Logger>)test.getClass();
		test.info("Logging initialization using the Autolog Manager");
		
		//map to the correct logging wrapper using the autolog configuration
		String implementationName = clazz.getCanonicalName();
		test.info("Logging implementation detected: {}", implementationName);
		String wrapperClass = logSettings.getProperty(implementationName + SEPARATOR + Manager.MANAGER_CLASS);
		test.info("Logging redirect to: {}", wrapperClass);
		String configFile = logSettings.getProperty(implementationName + SEPARATOR + Manager.CONFIG_FILE);
		test.info("Logging using configuration: {}", configFile);
		String settingsNamespace = logSettings.getProperty(implementationName + SEPARATOR + Manager.MANAGER_NAMESPACE);
		test.info("Logging from settings namespace: {}", settingsNamespace);
		
		//instantiate the logging wrapper
		test.info("Preparing to instantiate the logging redirect implementation. Logging will continue as defined by the target implementation.");
		redirect = Manager.getInstance(settings, wrapperClass, configFile, settingsNamespace, false);
	}

	/* (non-Javadoc)
	 * @see com.scheduler.logging.LogManager#finalize(com.scheduler.core.Settings)
	 */
	@Override
	public void finalize(Settings settings) {
		redirect.finalize(settings);
		redirect = null;
		logSettings = null;
	}

	/* (non-Javadoc)
	 * @see com.scheduler.logging.LogManager#getLogger(java.lang.String)
	 */
	@Override
	public Logger getLogger(String loggingNamespace) {
		return redirect.getLogger(loggingNamespace);
	}
}
